package Saas_Exercises;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Assignment07_RemovingZeroes {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		String temp = sc.nextLine(); // nextline

		Scanner sc1 = new Scanner(temp);
		int r = sc1.nextInt();
		int c = sc1.nextInt();
		System.out.println(r + " " + c);

		int[][] ori_array = new int[r][c]; // defining the mul-dim array

		for (int i = 0; i < r; i++) {
			if (sc.hasNext()) {

				String str = sc.nextLine(); // nextline
				Scanner sc2 = new Scanner(str);

				for (int j = 0; j < ori_array[i].length; j++) {

					ori_array[i][j] = sc2.nextInt(); // populating the mul-dim
														// array

				}

			} else {

				System.out.println("line not found");

			}

		} // for loop ends

		// displayArray(ori_array, r, c);

		removeZeroRowsColumns(ori_array, r, c);
		sc.close();
		sc1.close();
	}

	private static void removeZeroRowsColumns(int[][] ori_array, int r, int c) {

		int row_counter = 0;
		int col_counter = 0;
		int sum1 = 0;
		int sum2 = 0;

		List<Integer> rowID_List = new ArrayList<Integer>();
		List<Integer> colID_List = new ArrayList<Integer>();

		// traversing row by row

		while (row_counter < r) {

			col_counter = 0;
			sum1 = 0;

			while (col_counter < c) {
				sum1 = sum1 + ori_array[row_counter][col_counter];

				col_counter++;

			}

			if (sum1 == 0) {
				// System.out.println("Row_counter" + row_counter);
				// System.out.println("Row is zero");
				rowID_List.add(row_counter);
			} else {
				// System.out.println("Row is not zero");
			}
			row_counter++;

		}

		// traversing column by column

		col_counter = 0;
		while (col_counter < c) {
			row_counter = 0;
			sum2 = 0;
			while (row_counter < r) {
				sum2 = sum2 + ori_array[row_counter][col_counter];
				row_counter++;
			}

			if (sum2 == 0) {
				// System.out.println("Column is zero");
				// System.out.println("col_counter" + col_counter);
				colID_List.add(col_counter);

			} else {
				// System.out.println("Column not zero");

			}

			col_counter++;

		}

		displayFinalArray(ori_array, rowID_List, colID_List);

	}

	private static void displayFinalArray(int[][] ori_array,
			List<Integer> rowList, List<Integer> colList) {

		for (int i = 0; i < ori_array.length; i++) {

			if (!rowList.contains(i)) {

				for (int j = 0; j < ori_array[i].length; j++) {
					if (!colList.contains(j))

						System.out.print(ori_array[i][j] + " ");

				}
				System.out.println();
			}
		}

	}

	//this method not used
	private static void displayArray(int[][] array2, int row, int column) {

		for (int i = 0; i < row; i++) {
			for (int j = 0; j < column; j++) {

				System.out.print(array2[i][j] + " ");
			}

			System.out.println();
		}

	}

}
